/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ar.com.neolokura.travian.helper.world.admin;


import ar.com.neolokura.travian.helper.bo.item.OasisItem;
import ar.com.neolokura.travian.helper.constant.OasisContainer;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author gustavo
 */
public class WorldParser {
    
    public WorldParser() {
        
    }
    
    
    public OasisItem getOasisFromJson(String jsonString, Integer x, Integer y) {


        OasisItem oasis = null;

        // Takes out \n \t and \ from string
        jsonString = jsonString.replaceAll("\\\\n|\\\\t|\\\\", "");

        Pattern pattern = Pattern.compile("(?<=<span class=\"coordText\">).*?(?=</span>)");
        Matcher matcher = pattern.matcher(jsonString);


        Boolean isAnOasis = false;

        //Check if it's a free oasis
        while (matcher.find()) {
            if (matcher.group().equals(OasisContainer.UNNOCUPPIED_OASIS)) {
                isAnOasis = true;
            }
        }


        if (isAnOasis) {
            oasis = new OasisItem(x, y);
            // Gets All Troops and sets to the oasis
            HashMap<String, Integer> oasisTroops = getOasisTroopsFromJson(jsonString);
            ArrayList<Date> reports = getOasisReportsFromJson(jsonString);
            
            oasis.setBats(oasisTroops.get(OasisContainer.TROOP_BAT));
            oasis.setBears(oasisTroops.get(OasisContainer.TROOP_BEAR));
            oasis.setBoars(oasisTroops.get(OasisContainer.TROOP_BOAR));
            oasis.setCrocodiles(oasisTroops.get(OasisContainer.TROOP_CROCODILE));
            oasis.setElephants(oasisTroops.get(OasisContainer.TROOP_ELEPHANT));
            oasis.setRats(oasisTroops.get(OasisContainer.TROOP_RAT));
            oasis.setSnakes(oasisTroops.get(OasisContainer.TROOP_SNAKE));
            oasis.setSpiders(oasisTroops.get(OasisContainer.TROOP_SPIDER));
            oasis.setTigers(oasisTroops.get(OasisContainer.TROOP_TIGER));
            oasis.setWolfs(oasisTroops.get(OasisContainer.TROOP_WOLF));
            
            oasis.setReports(reports);
            
            

        }



        return oasis;
    }

    public HashMap<String, Integer> getOasisTroopsFromJson(String jsonString) {
        HashMap<String, Integer> oasisTroops = new HashMap<String, Integer>();

        for (String animal : OasisContainer.getOasisTroops()) {
            Integer qty = getAnimalQtyFromJson(jsonString, animal);
            oasisTroops.put(animal, qty);
        }

        return oasisTroops;
    }

    public Integer getAnimalQtyFromJson(String jsonString, String animal) {

        jsonString = jsonString.replaceAll("\\\\n|\\\\t|\\\\", "");
        Integer quantity = 0;
        Pattern pattern = Pattern.compile("(?<=<img class=\"" + animal + "\" src=\".{0,100}\" alt=\".{0,100}\" title=\".{0,100}\" /></td><td class=\"val\">).*?(?=</td>)");
        Matcher matcher = pattern.matcher(jsonString);

        while (matcher.find()) {
            try {
                quantity = Integer.parseInt(matcher.group());
            } catch (Exception e) {
            }
        }

        return quantity;
    }

    public ArrayList<Date> getOasisReportsFromJson(String jsonString) {
        ArrayList<Date> reports = new ArrayList<Date>();

        jsonString = jsonString.replaceAll("\\s", "");
        Pattern pattern = Pattern.compile("(?<=<ahref=\"berichte\\.php\\?id=.{0,500}\"title=\"\">).*?(?=</a>)");
        Matcher matcher = pattern.matcher(jsonString);

        while (matcher.find()) {
            if (!matcher.group().equals("")) {
                Date date;
                try {
                    date = convertDateString(matcher.group());
                    if (date != null) {
                        reports.add(date);
                    }

                } catch (ParseException ex) {
                }
            }
        }
        return reports;
    }

    public Date convertDateString(String dateString) throws ParseException{

        Calendar cal = Calendar.getInstance();
        SimpleDateFormat format = new SimpleDateFormat("yy/MM/ddHH:mm");


        if (dateString.contains("hoy") || dateString.contains("ayer")) {

            Boolean isYesterday = dateString.contains("ayer");
            Boolean isToday = dateString.contains("hoy");

            DateFormat dateFormat = new SimpleDateFormat("yy/MM/dd");
            dateString = dateString.replaceAll("hoy|ayer", "");

            if (isToday) {
                String today = dateFormat.format(cal.getTime());
                dateString = today + dateString;
            }
            if (isYesterday) {
                cal.add(Calendar.DATE, -1);
                String yesterday = dateFormat.format(cal.getTime());
                dateString = yesterday + dateString;
            }
        }
        cal.setTime(format.parse(dateString));
        return cal.getTime();
    }
}
